package org.example.Test;
import org.apache.ibatis.session.SqlSession;
import org.example.Mapper.UserMapper;
import org.example.MybatisUtils;
import org.example.PoJo.User;
import org.junit.Test;

public class UserMapperTest {
    //查询全部用户测试
//    @Test
//    public void test(){
//        //1.获得SqlSession对象
//        SqlSession sqlSession = MybatisUtils.getSqlSession();
//        //2.执行SQL
//        //方式一：getMapper
//        StudentMapper userMapper = sqlSession.getMapper(StudentMapper.class);
//        List<User> userList = userMapper.getStudentList();
//
//        //方式二：
//        //List<User> userList1 = sqlSession.selectList("com.zky.dao.UserDao.getUserList");
//        for (User user :userList
//        ) {
//            System.out.println(user);
//        }
//        sqlSession.close();
//    }
    //增加用户测试
//    @Test
//    public void addtext(){
//        SqlSession sqlSession = MybatisUtils.getSqlSession();
//        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//        User user = new User(3,"凡凡","666","年后");
//        mapper.addStudent(user);
//        //提交事务，增删改操作必须加上
//        sqlSession.commit();
//
//        sqlSession.close();
//    }
    //通过map测试
//    @Test
//    public void addtext2(){
//        SqlSession sqlSession = MybatisUtils.getSqlSession();
//        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//        Map<String, Object> map = new HashMap<String, Object>();
//        map.put("SID",6);
//        map.put("Sname","王大妈");
//        map.put("Class_","789");
//        map.put("School","新疆大学");
//        mapper.addStudent2(map);
//        //提交事务
//        sqlSession.commit();
//        //关闭资源
//        sqlSession.close();
//    }
    //删除用户测试
//    @Test
//    public void deletetext(){
//        SqlSession sqlSession = MybatisUtils.getSqlSession();
//
//        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//        int id = 4;
//        mapper.deleteStudent(id);
//
//        sqlSession.commit();
//
//        sqlSession.close();
//    }
    //更新用户测试
//    @Test
//    public void updatetext(){
//        SqlSession sqlSession = MybatisUtils.getSqlSession();
//
//        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//
//        User user = new User(1, "涵玉", "6","n");
//
//        mapper.updateStudent(user);
//
//        sqlSession.commit();
//
//        sqlSession.close();
//
//    }
    //查询单个用户测试
    @Test
    public void GetUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        String username = "11";
        String password="111";
        User user = mapper.GetUser(username,password);
        //查询不用提交事务
        System.out.println("user: "+user);
        sqlSession.close();
    }
    @Test
    public void GetUserList(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        System.out.println(mapper.getUserList());
    }
    //模糊查询
//    @Test
//    public void getUserLikeTest(){
//        SqlSession sqlSession = MybatisUtils.getSqlSession();
//
//        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//
//        String str = "王%";
//
//        List<User> userLike = mapper.getStudentLike(str);
//
//        for (User user :userLike
//        ) {
//            System.out.println(user);
//        }
//        sqlSession.close();
//    }
}
